package core.proto.rightwindow;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.HashSet;
import static app.constants.CollabVizConstants.*;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.colorchooser.AbstractColorChooserPanel;

import core.proto.InfoViz;
import core.proto.Util;

public class ColorChooser extends AbstractColorChooserPanel
implements ActionListener{
	 JToggleButton redCrayon;
	    JToggleButton yellowCrayon;
	    JToggleButton greenCrayon;
	    JToggleButton blueCrayon;
	    HashSet<JToggleButton> buttonContatiner = new HashSet<JToggleButton>();
	    
	    HashMap<String, Color> colorMap = new HashMap<String, Color>();

	    public void updateChooser() {
	        Color color = getColorFromModel();
	        
	        
	        if (Color.red.equals(color)) {
	            redCrayon.setSelected(true);
	        } else if (Color.yellow.equals(color)) {
	            yellowCrayon.setSelected(true);
	        } else if (Color.green.equals(color)) {
	            greenCrayon.setSelected(true);
	        } else if (Color.blue.equals(color)) {
	            blueCrayon.setSelected(true);
	        }
	    }

	    protected JToggleButton createCrayon(String name,
	                                         Border normalBorder) {
	        JToggleButton crayon = new JToggleButton();
	        crayon.setActionCommand(name);
	        crayon.addActionListener(this);

	        //Set the image or, if that's invalid, equivalent text.
	        ImageIcon icon = createImageIcon("images/" + name + ".gif");
	        if (icon != null) {
	            crayon.setIcon(icon);
	            crayon.setToolTipText("The " + name + " crayon");
	            crayon.setBorder(normalBorder);
	        } else {
	            crayon.setText("Image not found. This is the "
	                           + name + " button.");
	            crayon.setFont(crayon.getFont().deriveFont(Font.ITALIC));
	            crayon.setHorizontalAlignment(JButton.HORIZONTAL);
	            crayon.setBorder(BorderFactory.createLineBorder(Color.BLACK));
	        }

	        return crayon;
	    }

	    protected void buildChooser() {
	        setLayout(new GridLayout(0, 2));

	        String[] colors = {"green", "blue", "red", null};
	    	Color[] values = { new Color(0x6cf36c), new Color(195, 217, 255), new Color(0xff8997), null};
	    	
	    	if(InfoViz.UserID.equals("Alex")){
				colors[colors.length - 1] = "yellow";
				values[values.length - 1] = ALEX_COLOR;
			} else if(InfoViz.UserID.equals("Pat")){
				colors[colors.length - 1] = "orange";
				values[values.length - 1] = PAT_COLOR;
			}
	    	
	    	for(int i = 0; i < colors.length; i++){
	    		colorMap.put(colors[i], values[i]);
	    	}
	    	
	    	for(String color : colorMap.keySet()){
	    		JToggleButton b = new JToggleButton(new ImageIcon(Util.getServerResource("resources/img/colors/" + color + ".jpg")));
	    		b.setActionCommand(color);
	    		b.addActionListener(this);
	    		add(b);
	    		buttonContatiner.add(b);
	    		
	    		//Set default for user to be selected
	    		if(InfoViz.UserID.equals("Alex") && color.equals("yellow") ){
	    			b.setSelected(true);
				} else if(InfoViz.UserID.equals("Pat") && color.equals("orange")){
					b.setSelected(true);
				}
	    	}

	    }

	    /** Returns an ImageIcon, or null if the path was invalid. */
	    protected static ImageIcon createImageIcon(String path) {
	        java.net.URL imgURL = null;
	        if (imgURL != null) {
	            return new ImageIcon(imgURL);
	        } else {
	            System.err.println("Couldn't find file: " + path);
	            return null;
	        }
	    }

	    public void actionPerformed(ActionEvent e) {
	    	for(JToggleButton j : buttonContatiner){
	    		if(j !=(JToggleButton)e.getSource()){
	    			j.setSelected(false);
	    		}
	    	}
	    	
	    	//JToggleButton a = new JToggleButton();
	        Color newColor = null;
	        String command = ((JToggleButton)e.getSource()).getActionCommand();
	        System.out.println(command);
	        
	        newColor = colorMap.get(command);
	        
	        getColorSelectionModel().setSelectedColor(newColor);
	        
	        //Change color of selected postits
	    	for (PostIt postIt : BoardPane.getAllPostIts()) {
	    		if(postIt.isSelect() ){
	    			postIt.changeThisNoteColor(newColor);
	    		}
	    	}
	    }

	    public String getDisplayName() {
	        return "Crayons";
	    }

	    public Icon getSmallDisplayIcon() {
	        return null;
	    }

	    public Icon getLargeDisplayIcon() {
	        return null;
	    }

}
